<?php
/**
 * Created by PhpStorm.
 * User: liang
 * Email:1050187159@qq.com
 * Date: 2018/8/18
 * Time: 11:37
 */
 namespace Admin\Controller;

 class BannersController extends BaseController
 {

     private $banner = null;

     public function _initialize()
     {
         parent::_initialize();
         $this->banner = D('Banner');
     }

     public function index()
     {
         $banners = $this->banner->order('sort asc')->select();
         $this->assign(array(
             'banners' => $banners
         ));
         $this->display('Banners/list');
     }


     public function add()
     {
        if (!IS_POST){
            $this->display('Banners/add');
            exit;
        }

        if($this->banner->bannersAdd()){
            $this->ajaxReturn('true');
        } else {
            $this->ajaxReturn($this->banner->getError());
        }
     }

     public function modify()
     {
         $id = I('get.id');
         if (!IS_POST) {
             $banner = $this->banner->where(array('id'=>array('eq',$id)))->find();
             $this->assign(array(
                 'banner' => $banner
             ));
             $this->display('Banners/edit');
             exit;
         }
         if ($this->banner->bannersModify()) {
             $this->ajaxReturn('true');
         } else {
             $this->ajaxReturn($this->banner->getError());
         }
     }

     public function destroy()
     {
         $id = I('get.id');

         $banner = $this->banner->find($id);
         if (file_exists(APP_PATH.$banner['origin_image'])) {
             unlink(APP_PATH.$banner['origin_image']);
         }
         if (file_exists(APP_PATH.$banner['mobile_image'])) {
             unlink(APP_PATH.$banner['mobile_image']);
         }

         $result = $this->banner->where(array('id' => array('eq', $id)))->delete();

         if ($result !== false) {
             $this->ajaxReturn('true');
         } else {
             $this->ajaxReturn($this->banner->getError());
         }
     }

 }